package com.gyh.stlserver.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ListObjectsRequest;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Component
public class OssUtil {
    @Value("${ali.oss.endpoint}")
    private  String endpoint;
    @Value("${ali.oss.accessKeyId}")
    private  String accessKeyId;
    @Value("${ali.oss.accessKeySecret}")
    private  String accessKeySecret;
    @Value("${ali.oss.bucketName}")
    private  String bucketName;
    @Value("${ali.oss.contents}")
    private  String contents;
    @Value("${ali.oss.path}")
    private  String path;
    public  String upload(MultipartFile file) throws com.aliyuncs.exceptions.ClientException {
        String filename = file.getOriginalFilename();
        String objectName = contents+filename;
        //创建OSS对象
        OSS ossClient = null;
        try{
            ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            //上传
            ossClient.putObject(bucketName,objectName,file.getInputStream());
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            ossClient.shutdown();
        }
        return "https://zxst-2025.oss-cn-qingdao.aliyuncs.com/"+objectName;
    }
    public void cleanPicture(String dirPrefix,List<String> images){
        OSS ossClient = null;
        try{
            //创建OSS
            ossClient= new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            //获取oss上指定前缀下的所有文件
            ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName);
            //设定操作图片所在的文件夹的名称
            listObjectsRequest.setPrefix(contents+dirPrefix);
            //发送请求获取指定前缀下的文件
            ObjectListing objectListing = ossClient.listObjects(listObjectsRequest);
            //盛放oss上存储的文件的名称
            ArrayList<String> ossList = new ArrayList<>();
            //将对象中包含的文件信息单独存放到集合
            List<OSSObjectSummary> objectSummaries = objectListing.getObjectSummaries();
            for (OSSObjectSummary ossObjectSummary : objectSummaries) {
                ossList.add(path+ossObjectSummary.getKey());
            }
            for (String ossName : ossList) {
                boolean contains = images.contains(ossName);
                if (!contains){
                   String fileName = ossName.substring(ossName.lastIndexOf("/")+1,ossName.length());
                   String objectName = dirPrefix+ fileName;
                   ossClient.deleteObject(bucketName,objectName);
                    System.out.println("oss服务中图片:"+objectName+"被清除");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

    }
}
